#!/bin/sh
set - u

#服务名称
appName="etrading-query"

#程序部署工作空间
dir=$(dirname `readlink -f $0`)
cd $dir


# Error/GC 日志存放路径
# HEAP DUMP 日志存放路径
JVM_DEST_DIR=${HOME}/logs/tmp

if [ ! -d ${JVM_DEST_DIR} ];then
	mkdir -p ${JVM_DEST_DIR}
	chmod -R 755 ${JVM_DEST_DIR} 
fi


#日志大小
logOpts="-DlogFileSize=50MB"

#异步日志输出开关，不需要时，置空即可
asyncLogOpts="-Dlog4j2.contextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector"


# 64bit server mode
#显式强制要求使用server-64bit模式
SRV64_OPTS="-server -d64"

# 解锁实验性参数
UNLOCK_OPTS="-XX:+UnlockExperimentalVMOptions"

# G1GC 策略相关参数
#G1HeapRegionSize:格子大小,不小于4M;  MaxGCPauseMillis:最大暂停时间 默认值为200  高性能应用可以降低到100 单位ms 其他值均为默认
G1_OPTS="-XX:+UseG1GC -XX:G1HeapRegionSize=16M -XX:MaxGCPauseMillis=200 -XX:G1NewSizePercent=20 -XX:G1MaxNewSizePercent=60 -XX:ParallelGCThreads=4 -XX:InitiatingHeapOccupancyPercent=45 -XX:G1MixedGCLiveThresholdPercent=65 -XX:G1HeapWastePercent=10 -XX:G1MixedGCCountTarget=8 -XX:G1OldCSetRegionThresholdPercent=10 -XX:G1ReservePercent=10"

# Meta 相关设置
#MetaspaceSize:元数据区大小 默认值不修改
META_OPTS="-XX:MetaspaceSize=256M -XX:MaxMetaspaceSize=256M"

# HEAP 大小设置
#堆大小 前后值一致
HEAP_OPTS="-Xms256M -Xmx512M"

# JVM 报错输出设置
#异常报错时日志输出路径
ERR_LOG_OPTS="-XX:ErrorFile=${JVM_DEST_DIR}/java_err_%p_${appName}.log"

# GC 日志输出设置
#GC日志 UseGCLogFileRotation:启用日志分割  NumberOfGCLogFiles:日志个数    GCLogFileSize:日志大小  其余值默认
GC_LOG_OPTS="-Xloggc:${JVM_DEST_DIR}/java_gc_%p_${appName}.log -XX:+PrintGC -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=8 -XX:GCLogFileSize=64M -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintHeapAtGC -XX:+PrintGCApplicationConcurrentTime -XX:+PrintGCApplicationStoppedTime"

# HEAP DUMP 输出设置
#OOM时内存快照设定
DUMP_OPTS="-Djava.security.egd=file:/dev/./urandom -XX:+HeapDumpOnOutOfMemoryError -XX:+ExitOnOutOfMemoryError -XX:HeapDumpPath=${JVM_DEST_DIR}"

# JIT 设置
#ReservedCodeCacheSize:JIT空间 默认值
JIT_OPTS="-XX:ReservedCodeCacheSize=512M"

# 禁用显式GC调用
DISABLE_SYSGC_OPTS="-XX:+DisableExplicitGC"

# 激进优化
AGG_PERF_OPTS="-XX:+AggressiveOpts"

# 汇总JVM配置
JVM_OPTS="${SRV64_OPTS} ${UNLOCK_OPTS} ${G1_OPTS} ${META_OPTS} ${HEAP_OPTS} ${ERR_LOG_OPTS} ${GC_LOG_OPTS} ${DUMP_OPTS} ${JIT_OPTS} ${DISABLE_SYSGC_OPTS} ${AGG_PERF_OPTS}"

#业务参数
#AP_OPTS="-DarchEnableMetric=true"

# 汇总 JAVA 配置
JAVA_OPTS="${JVM_OPTS} ${AP_OPTS}"

#启动加载的主程序jar
mainJar=`find $dir -name "${appName}*.jar"`
if [ -z "$mainJar" ]; then
    echo "app[${appName}] not found"
    exit
fi
echo "正在启动的jar是:"${mainJar}


#防重启
PIDS=`ps -efww|grep "${mainJar}" |grep -v grep|awk '{print $2}'`
if [ "$PIDS" == "" ];then
	java -Deas.uri=192.168.10.45:13885 -Dappl.host=192.168.10.46 -DapplUrl=http://192.168.10.46:9001/ -DecasLoginUrl=http://192.168.10.46:9001/ecas -Dspring.cloud.nacos.config.namespace=test45 ${JAVA_OPTS} -Duser.timezone=Asia/Shanghai  -cp config:${mainJar} org.springframework.boot.loader.JarLauncher >/dev/null  &
	echo "${appName} start sucess!"
else
	echo "${appName} is running..."
fi

echo "0"